package com.yuqing.myblogServe.service.impl;

import com.github.pagehelper.PageHelper;
import com.yuqing.myblogServe.common.lang.PageBean;
import com.yuqing.myblogServe.common.lang.PageBuilder;
import com.yuqing.myblogServe.dao.entity.Article;
import com.yuqing.myblogServe.dao.entity.ArticleRank;
import com.yuqing.myblogServe.dao.mapper.ArticleCommentMapper;
import com.yuqing.myblogServe.dao.mapper.ArticleMapper;
import com.yuqing.myblogServe.dto.ArticleCommentDto;
import com.yuqing.myblogServe.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuqing.myblogServe.utils.JwtUtils;
import com.yuqing.myblogServe.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import sun.security.acl.PrincipalImpl;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuqing
 * @since 2023-12-01
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    JwtUtils jwtUtils;


    Principal principal;

    @Autowired
    TimeUtil timeUtil;

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArticleCommentMapper articleCommentMapper;


    @Override
    public Article getArticleByArtId(String articleId) {

        return articleMapper.getArticleByArticleId(articleId);
    }

    @Override
    public boolean publishArticle(Article article) {

        UUID uuid = UUID.randomUUID();

        article.setArticleId(uuid.toString());


        article.setCommentCount(0);

        article.setLikes(0);

        article.setPublishDate(LocalDateTime.now());

        article.setUpdateDate(LocalDateTime.now());

        return save(article);
    }

    @Override
    public int updateArticle(Article article) {


        article.setUpdateDate(LocalDateTime.now());

        return articleMapper.updateById(article);

    }

    @Override
    public List<ArticleRank> getLikesRank() {
        return articleMapper.getArticleLikesRank();
    }

    @Override
    public int updateLickArticle(String articleId) {
        int count = articleMapper.updateLikesById(articleId);
        return count;
    }

    @Override
    public PageBean<ArticleCommentDto> listCommentByPage(Long userId, String articleId, int page, int pageSize) {

        PageHelper.startPage(page, pageSize,"update_time desc");

        return PageBuilder.build(articleCommentMapper.listCommentByPage(userId, articleId));
    }
}
